/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.patient.medicationtask;

import be.ugent.medirem.logic.MeasurementRemote;
import be.ugent.medirem.logic.MedicationRemote;
import be.ugent.medirem.logic.PatientRemote;
import be.ugent.medirem.logic.TaskRemote;
import be.ugent.medirem.persistence.entity.details.MeasurementDetails;
import be.ugent.medirem.persistence.entity.details.MedicationTaskDetails;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.util.TaskStatus;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class ShowMedicationTaskServlet extends HttpServlet {
    @EJB
    private MedicationRemote medicationBean;
    @EJB
    private PatientRemote patientBean;
    @EJB
    private MeasurementRemote measurementBean;
    @EJB
    private TaskRemote taskBean;

   
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        Long id = Long.parseLong(request.getParameter("id"));
        if (id == 0)
            response.sendRedirect("/patient/medicationtask/list");
        else {
            MedicationTaskDetails mtd = null;
            try {
                mtd = taskBean.getTaskDetails(id);
                if (mtd != null && mtd.getStatus() == TaskStatus.NOT_SEEN)
                    taskBean.updateTaskStatus(mtd.getId(), id, TaskStatus.NOT_COMPLETED);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(ShowMedicationTaskServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (EntityNotFoundException ex) {
                Logger.getLogger(ShowMedicationTaskServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (mtd != null && !mtd.getPrescription().isDosageIsFixed())
                request.setAttribute("unit", measurementBean.getMeasurementTypeUnit(mtd.getPrescription().getMedication().getMeasurementType()));

            String username = request.getUserPrincipal().getName();
            id = patientBean.getPatientId(username);
            List<MeasurementDetails> measurements = null;
            try {
                measurements = measurementBean.getMeasurements(id, mtd.getPrescription().getMedication().getMeasurementType());
            } catch (EJBException e) {
            }
            request.setAttribute("task", mtd);
            request.setAttribute("measurements", measurements);
            request.getRequestDispatcher("/patient/medicationtask/show.jsp").forward(request, response);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
